<?php
if(!defined('IN_GAME')) exit('Access Denied');
define('TIMESTAMP', time());
error_reporting(E_ALL ^ E_NOTICE);

session_start();
ob_start();

if(!isset($_GET['testing']) && !isset($_SESSION['testing']) && date('YmdH')<'2015101810'){
    header('location:wait-for-coming/');
    exit();
}

if(isset($_GET['testing'])) 
	$_SESSION['testing'] = $_GET['testing'];

if($_SESSION['userid']){
	db::init();
	$_ENV['user'] = db::fetch_first("SELECT * FROM hgame_user WHERE userid='%d'", $_SESSION['userid']);
}else{
	$_ENV['user'] = array();
}

function userget($name){
	return $_ENV['user'][$name];
}

function userset($name, $value){
	$_ENV['user'][$name] = $value;
}

class db{
	public static $con;
	public static function init(){
		/*
		* If you arrive here, please tell us, you will have the qualification to enter the second round.
		* actually we backup everything...
		*/
		if($con) return;
		self::$con = mysqli_connect('127.0.0.1', 'c184sec', 'sec27756','c184sec');

		mysqli_query(self::$con, "SET CHARACTER SET 'utf8'");
		mysqli_query(self::$con,"SET NAMES 'utf8'");
	}
	
	public static function query(){
		$args = func_get_args();
		$string = $args[0];
		array_shift($args);
		
		if(count($args) == 0)
			$sql = $string;
		else
			$sql = vsprintf($string, $args);
		
		$query = mysqli_query(self::$con, $sql);
		
		if(mysqli_error(self::$con))
			echo mysqli_error(self::$con);
		
		return $query;
	}
	
	public static function fetch_first(){
		$args = func_get_args();
		$string = $args[0];
		array_shift($args);
		
		if(count($args) == 0)
			$sql = $string;
		else
			$sql = vsprintf($string, $args);
		
		$query = mysqli_query(self::$con, $sql);
		
		if(mysqli_error(self::$con))
			echo mysqli_error(self::$con);
		
		return self::fetch($query);
	}
	
	public static function fetch($query){
		return mysqli_fetch_array($query, MYSQLI_ASSOC);
	}
}
